package com.yh.workbill.ui.leaseInfo.mvp;

import com.yh.workbill.model.Company;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.ui.companyInfo.CompanyListInfoViewModel;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Model;
import com.yh.workbill.ui.leaseInfo.LeaseInfoViewModel;

import java.util.List;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class AddLease_Presenter implements LifecycleObserver {
    private AddLease_View view;
    private AddLease_Model model;

    public AddLease_Presenter(AddLease_View view) {
        this.view = view;
        this.model = new AddLease_Model();
    }


    public void add(String work_use_company) {
        view.showDialog("正在添加数据中...", false);
        model.addUnitName(work_use_company, new AddLease_Model.AddLeaseCallback() {
            @Override
            public void onSuccess(String name) {
                view.hideDialog();
                view.addSuccess(name);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.showTasot(msg);
            }
        });
    }

    public void getQueryData(LeaseInfoViewModel viewModel, boolean isRefresh, boolean isLoadMore, int pageindex, int pagenumber) {
        view.showDialog("正在查询数据", true);
        model.getAllUseCompany(pageindex, pagenumber, new AddLease_Model.UseCompanyCallback() {
            @Override
            public void SuccessCompany(List<UseCompany> data) {
                view.hideDialog();
                UseCompany[] useCompanies = data.toArray(new UseCompany[data.size()]);
                if (isRefresh) {
                    if (viewModel.getUseCompanys().getValue() != null) {
                        viewModel.getUseCompanys().getValue().clear();
                    }
                    viewModel.addUseCompanydatas(0, useCompanies);
                    view.refresh();
                } else if (isLoadMore) {
                    int index = viewModel.getUseCompanys().getValue().size();
                    viewModel.addUseCompanydatas(index, useCompanies);
                    view.loadMore();
                }
                view.getData(data);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.getDataFail(msg);
            }
        });
    }

    public void delUseCompany(String company_name, String uuid) {
        view.showDialog("正在删除 " + company_name + " 信息", true);
        model.delUseCompany(company_name, uuid, new AddCompanyInfo_Model.DelCallBacl() {
            @Override
            public void onSuceess(String company) {
                view.hideDialog();
                view.delSuccess(company);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.delFail(msg);
            }
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestory() {
        if (this.view != null) {
            this.view = null;
        }
    }
}
